//package com.pengzy.core.cloud_security;
//
//import cn.hutool.jwt.JWTUtil;
//import com.alibaba.fastjson.JSONObject;
//import com.fasterxml.jackson.databind.ObjectMapper;
//import com.pengzy.comment.core.jwt.JwtUtil;
//import com.pengzy.comment.web.result.JsonResult;
//import lombok.SneakyThrows;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.core.io.buffer.DataBuffer;
//import org.springframework.http.HttpHeaders;
//import org.springframework.http.ResponseCookie;
//import org.springframework.http.server.reactive.ServerHttpResponse;
//import org.springframework.security.core.Authentication;
//import org.springframework.security.core.GrantedAuthority;
//import org.springframework.security.web.server.WebFilterExchange;
//import org.springframework.security.web.server.authentication.WebFilterChainServerAuthenticationSuccessHandler;
//import org.springframework.stereotype.Component;
//import org.springframework.web.server.ServerWebExchange;
//import reactor.core.publisher.Mono;
//
//import java.util.HashMap;
//import java.util.List;
//import java.util.Map;
//import java.util.stream.Collectors;
//
///**
// * 定义认证成功类
// * @author xiaozhi 2023/7/17
// */
//
//@Component
//@Slf4j
//public class AuthenticationSuccessHandler extends WebFilterChainServerAuthenticationSuccessHandler {
//
//
//    @SneakyThrows
//    @Override
//    public Mono<Void> onAuthenticationSuccess(WebFilterExchange webFilterExchange, Authentication authentication) {
//        log.info("认证成功类");
//        ServerWebExchange exchange = webFilterExchange.getExchange();
//        ServerHttpResponse response = exchange.getResponse();
////        //设置headers
//        HttpHeaders httpHeaders = response.getHeaders();
//        httpHeaders.add("Content-Type", "application/json; charset=UTF-8");
//        httpHeaders.add("Cache-Control", "no-store, no-cache, must-revalidate, max-age=0");
////        //设置body
////        HashMap<String, String> map = new HashMap<>();
////        String remember_me=exchange.getRequest().getHeaders().getFirst("Remember-me");
////
//        ObjectMapper mapper = new ObjectMapper();
////        List<? extends GrantedAuthority> list= (List<? extends GrantedAuthority>) authentication.getAuthorities();
////        try {
////            Map<String, String> load = new HashMap<>();
////            load.put("username",authentication.getName());
////            load.put("role",list.get(0).getAuthority());//这里只添加了一种角色 实际上用户可以有不同的角色类型
////            String token;
////            authentication.getPrincipal()
////            log.info(authentication.toString());
////            if (remember_me==null) {
////                token= JwtUtil.createJWT(authentication.getName(),3600*24L);
////                response.addCookie(ResponseCookie.from("token", token).path("/").build());
////                //maxAge默认-1 浏览器关闭cookie失效
////                redisTemplate.opsForValue().set(authentication.getName(), token, 1, TimeUnit.DAYS);
////            }else {
////                token=JWTUtils.creatToken(load,3600*24*180);
////                response.addCookie(ResponseCookie.from("token", token).maxAge(Duration.ofDays(rememberMe)).path("/").build());
////                redisTemplate.opsForValue().set(authentication.getName(), token, rememberMe, TimeUnit.SECONDS);//保存180天
////            }
////
////            map.put("code", "000220");
////            map.put("message", "登录成功");
////            map.put("token",token);
////        } catch (Exception ex) {
////            ex.printStackTrace();
////            map.put("code", "000440");
////            map.put("message","登录失败");
////        }
//        DataBuffer bodyDataBuffer = response.bufferFactory().wrap(JSONObject.toJSONBytes(JsonResult.error("登录成功")));
//        return response.writeWith(Mono.just(bodyDataBuffer));
//    }
//}
